<template>
    <div>
        <el-form :model="modifyForm" status-icon :rules="rules" ref="modifyForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="手机号码" prop="phoneNumber">
                <el-input type="text" v-model="modifyForm.phoneNumber" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="新密码" prop="newPassword">
                <el-input type="password" v-model="modifyForm.newPassword" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="确认密码" prop="reNewPassword">
                <el-input type="password" v-model="modifyForm.reNewPassword"></el-input>
            </el-form-item>
            <el-form-item label="手机验证码" prop="phoneCode" class="code">
                <el-input v-model="modifyForm.phoneCode"></el-input>
                <el-button v-if="!sending" class="getCodeBtn" @click="getCode">获取验证码</el-button>
                <el-button v-else disabled key="disabled" class="getCodeBtn disabled">
                    {{ leftSecond }}秒后重发
                </el-button>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submitForm">提交</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>
<script>
export default {
    name: 'Modify',
    data() {
        const checkPhone = (rule, value, callback) => {
            value = value.trim()
            let reg_tel = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
            if (!value) {
                this.modifyForm.phoneNumber = ''
                return callback(new Error('手机号码不能为空'));
            }
            else if (!reg_tel.test(value)) {
                callback(new Error('请正确填写您的手机号码!'));
            }
            else {
                callback();
            }
        };
        const validatePass = (rule, value, callback) => {
            if (value.trim() === '') {
                this.modifyForm.newPassword = ''
                callback(new Error('请输入密码'));
            } else if (value.length < 6) {
                callback(new Error('密码长度不能小于6位数!'));
            }
            else {
                if (this.modifyForm.reNewPassword !== '') {
                    this.$refs.modifyForm.validateField('reNewPassword');
                }
                callback();
            }
        };
        const validatePass2 = (rule, value, callback) => {
            value = value.trim()
            if (value === '') {
                this.modifyForm.reNewPassword = ''
                callback(new Error('请再次输入密码'));
            } else if (value !== this.modifyForm.newPassword) {
                callback(new Error('两次输入密码不一致!'));
            } else {
                callback();
            }
        };
        const checkCode = (rule, value, callback) => {
            if (value.trim() === '') {
                this.modifyForm.phoneCode = ''
                callback(new Error('请输入验证码'));
            } else {
                callback();
            }
        };
        return {
            modifyForm: {
                phoneNumber: '13924763187',
                newPassword: '',
                reNewPassword: '',
                phoneCode: ''
            },
            rules: {
                phoneNumber: [
                    { validator: checkPhone, trigger: "blur" }
                ],
                newPassword: [
                    { validator: validatePass, trigger: 'blur' }
                ],
                reNewPassword: [
                    { validator: validatePass2, trigger: 'blur' }
                ],
                phoneCode: [
                    { validator: checkCode, trigger: 'blur' }
                ]
            },
            leftSecond: 0,
            sending: false,
        };
    },
    methods: {
        submitForm() {
            this.$refs.modifyForm.validate(async (valid) => {
                if (valid) {
                    const result = await this.$API.userList.reqUpdatePassword(this.modifyForm).catch(() => {
                        this.$message.error('修改失败');
                    })
                    console.log(result)
                    if (result.resultCode == 200) {
                        this.$message.success('修改成功!')
                        await this.$store.dispatch("logout");
                        this.$router.push(`/login`);
                    } else {
                        this.$message.error('修改失败');
                    }
                } else {
                    return false;
                }
            });
        },
        async getCode() {
            try {
                const phone = this.modifyForm.phoneNumber;
                if (phone) {
                    const result = await this.$API.userList.reqGetPhoneCode({ phone }).catch(() => {
                        this.$message.error('发送失败');
                    })
                    console.log(result)
                    if (result.resultCode == 200) {
                        this.$message.success('发送成功!')
                        //倒计时
                        this.sending = true;
                        // this.leftSecond = 5;
                        this.leftSecond = 60;
                        //创建定时器
                        this.timmer = setInterval(() => {
                            //计数器减一
                            this.leftSecond--;
                            if (this.leftSecond <= 0) {
                                //停止定时器
                                clearInterval(this.timmer);
                                //还原计数器
                                this.leftSecond = 0;
                                //还原按钮状态
                                this.sending = false;
                            }
                        }, 1000);
                    } else {
                        this.$message.error('发送失败');
                    }
                } else {
                    this.$message.error('请输入手机号码');
                };

            } catch (error) {
                this.$message.error('获取失败');
            }
        }
    },
    beforeDestroy() {
        //清除定时器
        clearInterval(this.timmer);
    }

};
</script>
<style lang="scss" scoped>
.code {
    width: 80%;
    position: relative;

    .getCodeBtn {
        position: absolute;
    }
}
</style>